package com.ihighest.app.infox.adapter;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.infox.R;

/**
 * 我的订阅适配器
 * 
 * @author lkx Created at 2015-11-11 下午5:14:16
 * @version 1.0
 */
public class SelecMyColumnListAdapter extends BaseAdapter {

	private Context context;
	private JSONArray jsonArray;
	private LayoutInflater inflater;
	private String url;
	int flag;
	String id;

	public SelecMyColumnListAdapter() {
	}

	public SelecMyColumnListAdapter(Context context) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
	}

	public SelecMyColumnListAdapter(Context context, JSONArray jsonArray) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.jsonArray = jsonArray;
		// this.flag = flag;

	}

	@Override
	public int getCount() {
		return jsonArray == null ? 0 : jsonArray.length();
	}

	@Override
	public Object getItem(int arg0) {
		try {
			return jsonArray.get(arg0);
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.infox_activity_subscription_item, null);
			holder.selecColumnImg = (ImageView) convertView.findViewById(R.id.selecColumnImg);
			holder.selecColumnTitle = (TextView) convertView.findViewById(R.id.selecColumnTitle);
			holder.selecColumnIntroduction = (TextView) convertView.findViewById(R.id.selecColumnIntroduction);
			holder.selecColumnMessageCount = (TextView) convertView.findViewById(R.id.selecColumnMessageCount);
			holder.selecColumnSubscription = (TextView) convertView.findViewById(R.id.selecColumnSubscription);
			holder.selecColumncount = (TextView) convertView.findViewById(R.id.id_count);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		// BadgeView surveyBadge = new BadgeView(context,
		// holder.selecColumnImg);
		// surveyBadge.setText("1");
		// surveyBadge.setTextSize(11);
		// surveyBadge.setGravity(Gravity.CENTER);
		// surveyBadge.setBackgroundResource(R.drawable.infox_orange_roungbg);
		// surveyBadge.setTextColor(Color.parseColor("#fa9600"));
		// surveyBadge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);

		try {
			holder.selecColumnTitle.setText(jsonArray.getJSONObject(position).getString("title"));
			holder.selecColumnIntroduction.setText(jsonArray.getJSONObject(position).getString("memo"));
			url = jsonArray.getJSONObject(position).getString("image");
			id = jsonArray.getJSONObject(position).getString("id");
			int count = jsonArray.getJSONObject(position).getInt("count");
			if (count == 0) {
				holder.selecColumncount.setVisibility(View.GONE);

				// surveyBadge.hide(false);
			} else {

				holder.selecColumncount.setText("订阅人数：" + count);
				holder.selecColumncount.setVisibility(View.VISIBLE);
				// surveyBadge.setText(count + "");
				// surveyBadge.show();
			}

			// DisplayImageOptions defaultOptions = new
			// DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.infox_img_loadrong)
			// .showImageOnFail(R.drawable.infox_img_loadrong).showImageOnLoading(R.drawable.infox_img_loadrong).cacheInMemory(true)
			// .cacheOnDisc(true).build();
			// ImageLoader.getInstance().displayImage(url,
			// holder.selecColumnImg, defaultOptions);
			AppKit.SetNormalImg(url, holder.selecColumnImg);
			if (Boolean.valueOf(jsonArray.getJSONObject(position).getString("isSubscribes"))) {
				holder.selecColumnSubscription.setBackgroundResource(R.drawable.infox_searchitem_third);
				holder.selecColumnSubscription.setTextColor(Color.parseColor("#ffffff"));
				holder.selecColumnSubscription.setText("已订阅");
			} else {
				holder.selecColumnSubscription.setBackgroundResource(R.drawable.infox_orange_frame);
				holder.selecColumnSubscription.setTextColor(Color.parseColor("#fa6400"));
				holder.selecColumnSubscription.setText("订阅");
			}
			holder.selecColumnSubscription.setTag(R.id.tag_1, jsonArray.getJSONObject(position).getString("isSubscribes"));
			holder.selecColumnSubscription.setTag(R.id.tag_2, jsonArray.getJSONObject(position).getString("id"));
			holder.selecColumnSubscription.setTag(R.id.tag_3, jsonArray.getJSONObject(position).getString("type"));
			// holder.selecColumnSubscription.setOnClickListener(new
			// OnClickListener() {
			// @Override
			// public void onClick(View view) {
			// //是否登陆
			// if (TextUtils.isEmpty(SharedPreferencesKit.getString(context,
			// Const.SP_KEY_USER_ID))) {
			// Intent intent = new Intent(context, LoginActivity.class);
			// context.startActivity(intent);
			// return ;
			// }
			//
			//
			//
			// final String state = (String) view.getTag(R.id.tag_1);
			// String id = (String) view.getTag(R.id.tag_2);
			// String type = (String) view.getTag(R.id.tag_3);
			// String url = "Columnist".equalsIgnoreCase(type) ?
			// "me/followColumnist" : "me/followEnterprise";
			// Api.post(url, ApiParam.create().addParam("id",
			// id).addParam("type", Boolean.valueOf(state) ? "2" : "1"),
			// new ApiCallback(context, true) {
			//
			// @Override
			// public void handleSuccess(ApiResult result) {
			// if (result.getCode() == 100) {
			// if (Boolean.valueOf(state)) {
			// holder.selecColumnSubscription.setTag(R.id.tag_1, "false");
			// holder.selecColumnSubscription.setBackgroundResource(R.drawable.infox_orange_frame);
			// holder.selecColumnSubscription.setTextColor(Color.parseColor("#fa6400"));
			// holder.selecColumnSubscription.setText("订阅");
			// } else {
			// holder.selecColumnSubscription.setTag(R.id.tag_1, "true");
			// holder.selecColumnSubscription.setBackgroundResource(R.drawable.infox_searchitem_third);
			// holder.selecColumnSubscription.setTextColor(Color.parseColor("#ffffff"));
			// holder.selecColumnSubscription.setText("已订阅");
			// }
			// }
			// }
			//
			// @Override
			// public void handleFailure(Exception e, String msg) {
			// }
			// }, context);
			// }
			// });
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
		return convertView;
	}

	class ViewHolder {
		ImageView selecColumnImg;
		TextView selecColumnTitle;
		TextView selecColumnIntroduction;
		TextView selecColumnMessageCount;
		TextView selecColumnSubscription;
		TextView selecColumncount;

	}

	/**
	 * 替换当前数据源数据
	 * 
	 * @param moreArry
	 * 
	 * @author lkx Created at 2015-10-20 下午8:49:23
	 */
	public void setData(JSONArray jsonArray) {
		this.jsonArray = jsonArray;
		notifyDataSetChanged();
	}

	/**
	 * 向已有数据源中追加数据
	 * 
	 * @param jsonArray
	 * 
	 * @author lkx Created at 2015-10-20 下午8:49:00
	 */
	public void appendData(JSONArray jsonArray) {
		try {
			this.jsonArray = AppKit.joinJSONArray(this.jsonArray, jsonArray);
		} catch (JSONException e) {
		}
		notifyDataSetChanged();
	}
}